package com.th.supcom.pds.impl.service.internal;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.carefx.framework.exception.BusinessException;
import net.carefx.framework.translate.TranslatorManager;

import org.apache.cxf.common.util.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.th.supcom.pds.domain.pojo.PdsSupply;
import com.th.supcom.pds.domain.pojo.PhamWindowConfig;
import com.th.supcom.pds.domain.vo.PdsSupplyVO;
import com.th.supcom.pds.impl.common.enums.AvailableEnum;
import com.th.supcom.pds.impl.manager.PdsSupplyManager;
import com.th.supcom.pds.impl.manager.PhamWindowConfigManager;
import com.th.supcom.pds.internal.api.PdsSupplyService;

/* $Id$
 *
 * Copyright ( c ) 2014 XuNuo Corporation. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of XuNuo
 * Corporation ("Confidential Information").  You shall not disclose such
 * Confidential Information and shall use it only in accordance with the terms
 * of the license agreement you entered into with XuNuo Corporation or a XuNuo
 * authorized reseller (the "License Agreement"). XuNuo may make changes to the
 * Confidential Information from time to time. Such Confidential Information may
 * contain errors.
 *
 * EXCEPT AS EXPLICITLY SET FORTH IN THE LICENSE AGREEMENT, XuNuo DISCLAIMS ALL
 * WARRANTIES, COVENANTS, REPRESENTATIONS, INDEMNITIES, AND GUARANTEES WITH
 * RESPECT TO SOFTWARE AND DOCUMENTATION, WHETHER EXPRESS OR IMPLIED, WRITTEN OR
 * ORAL, STATUTORY OR OTHERWISE INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A
 * PARTICULAR PURPOSE. XuNuo DOES NOT WARRANT THAT END USER'S USE OF THE
 * SOFTWARE WILL BE UNINTERRUPTED, ERROR FREE OR SECURE.
 *
 * XuNuo SHALL NOT BE LIABLE TO END USER, OR ANY OTHER PERSON, CORPORATION OR
 * ENTITY FOR INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL
 * DAMAGES, OR DAMAGES FOR LOSS OF PROFITS, REVENUE, DATA OR USE, WHETHER IN AN
 * ACTION IN CONTRACT, TORT OR OTHERWISE, EVEN IF XuNuo HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES. XuNuo' TOTAL LIABILITY TO END USER SHALL NOT
 * EXCEED THE AMOUNTS PAID FOR THE XuNuo SOFTWARE BY END USER DURING THE PRIOR
 * TWELVE (12) MONTHS FROM THE DATE IN WHICH THE CLAIM AROSE.  BECAUSE SOME
 * STATES OR JURISDICTIONS DO NOT ALLOW LIMITATION OR EXCLUSION OF CONSEQUENTIAL
 * OR INCIDENTAL DAMAGES, THE ABOVE LIMITATION MAY NOT APPLY TO END USER.
 *
 * Copyright version 2.0
 /**
 * 处方主记录Service
 * @author 杨冲
 */
public class PdsSupplyServiceImpl implements PdsSupplyService
{

    private PdsSupplyManager m_pdsSupplyManager;
    private TranslatorManager m_translatorManager;
    private PhamWindowConfigManager m_phamWindowConfigManager;

    public void setPdsSupplyManager (PdsSupplyManager pdsSupplyManager)
    {

        this.m_pdsSupplyManager = pdsSupplyManager;
    }

    public void setTranslatorManager (TranslatorManager translatorManager)
    {
        m_translatorManager = translatorManager;
    }

    public void setPhamWindowConfigManager (PhamWindowConfigManager phamWindowConfigManager)
    {
        m_phamWindowConfigManager = phamWindowConfigManager;
    }

    /**
     * 处方持久化
     */
    @Override
    public void persistentPdsSupply (PdsSupply pdsSupply)
    {
        s_logger.info ("调用persistentPdsSupplyf方法：入参:{}", new Object[]
        { pdsSupply });
        try
        {
            m_pdsSupplyManager.addPdsSupply (pdsSupply);
        }
        catch (Exception e)
        {
            s_logger.error ("处方持久化异常，异常信息：", new Object[]
            { e });
            throw new BusinessException ("数据库插入出现异常" + e.getMessage (), e);
        }

    }

    /** 日志记录器. */
    public static final Logger s_logger = LoggerFactory.getLogger (PdsSupplyServiceImpl.class);

    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, XuNuo Corporation";

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";

    @Override
    public void modifyPdsSupplyByExample (PdsSupply pdsSupply)
    {
        s_logger.debug ("进入modifyPdsSupplyByExample方法,入参:");

        try
        {
            m_pdsSupplyManager.modifyPdsSupplyByExample (pdsSupply);
        }
        catch (Exception e)
        {
            s_logger.error (e.getLocalizedMessage ());
            throw new BusinessException ("修改处方主记录出错，请联系计算机中心！");
        }
    }

    @Override
    public PdsSupply getPdsSupplyByID (Long id)
    {
        s_logger.info ("调用 getPdsSupplyByID (Long id = 【{}】", new Object[]
        { id });
        try
        {
        }
        catch (Exception e)
        {
            s_logger.error (e.getLocalizedMessage ());
            throw new BusinessException ("查询供药信息主表出错");
        }
        return null;
    }

    @Override
    public Long getPdsSupplySeq ()
    {
        s_logger.info ("调用 getPdsSupplySeq");
        try
        {
            return m_pdsSupplyManager.getPdsSupplySeq ();
        }
        catch (Exception e)
        {
            s_logger.error (e.getLocalizedMessage ());
            throw new BusinessException ("查询供药信息主表出错");
        }
    }

    @Override
    public List <PdsSupply> findPdsSupplyListByExample (PdsSupply pdsSupply)
    {
        s_logger.info ("调用 findPdsSupplyListByExample (PdsSupply pdsSupply = 【{}】", new Object[]
        { pdsSupply });
        try
        {
            return m_pdsSupplyManager.findPdsSupplyListByExample (pdsSupply);
        }
        catch (Exception e)
        {
            s_logger.error (e.getLocalizedMessage ());
            throw new BusinessException ("查询供药信息主表出错");
        }
    }

    @Override
    public int findPdsSupplyCountByExample (PdsSupply pdsSupply)
    {
        s_logger.info ("调用 findPdsSupplyCountByExample (PdsSupply pdsSupply = 【{}】", new Object[]
        { pdsSupply });
        try
        {
            return m_pdsSupplyManager.findPdsSupplyCountByExample (pdsSupply);
        }
        catch (Exception e)
        {
            s_logger.error (e.getLocalizedMessage ());
            throw new BusinessException ("查询供药信息主表出错");
        }
    }

    @Override
    public List <PdsSupply> findPdsSupplyListByMap (Map <String, Object> params)
    {
        s_logger.info ("调用 findPdsSupplyListByMap ((Map <String, Object> params = 【{}】", new Object[]
        { params });
        try
        {
            return m_pdsSupplyManager.findPdsSupplyListByMap (params);
        }
        catch (Exception e)
        {
            s_logger.error (e.getLocalizedMessage ());
            throw new BusinessException ("查询供药信息主表出错");
        }
    }

    @Override
    public List <PdsSupply> findPdsSupplyListWithPageByMap (Map <String, Object> params, Integer offset, Integer limit)
    {
        s_logger.info ("调用 findPdsSupplyListWithPageByMap (Map <String, Object> params, Integer offset, Integer limit = 【{}】", new Object[]
        { params, offset, limit });
        try
        {
            return m_pdsSupplyManager.findPdsSupplyListWithPageByMap (params, offset, limit);
        }
        catch (Exception e)
        {
            s_logger.error (e.getLocalizedMessage ());
            throw new BusinessException ("查询供药信息主表出错");
        }
    }

    @Override
    public Integer findPdsSupplyCountByMap (Map <String, Object> params)
    {
        s_logger.info ("调用 findPdsSupplyCountByMap (Map <String, Object> params = 【{}】", new Object[]
        { params });
        try
        {
            return m_pdsSupplyManager.findPdsSupplyCountByMap (params);
        }
        catch (Exception e)
        {
            s_logger.error (e.getLocalizedMessage ());
            throw new BusinessException ("查询供药信息主表出错");
        }
    }

    @Override
    public int modifyPdsSupply (PdsSupply pdsSupply)
    {
        s_logger.info ("调用 modifyPdsSupply (PdsSupply pdsSupply = 【{}】", new Object[]
        { pdsSupply });
        try
        {
            return m_pdsSupplyManager.modifyPdsSupply (pdsSupply);
        }
        catch (Exception e)
        {
            s_logger.error (e.getLocalizedMessage ());
            throw new BusinessException ("修改处方主记录出错");
        }
    }

    @Override
    public Long addPdsSupply (PdsSupply pdsSupply)
    {
        s_logger.info ("调用 addPdsSupply (PdsSupply pdsSupply = 【{}】", new Object[]
        { pdsSupply });
        try
        {
            return m_pdsSupplyManager.addPdsSupply (pdsSupply);
        }
        catch (Exception e)
        {
            s_logger.error (e.getLocalizedMessage ());
            throw new BusinessException ("新增供药信息主表出错");
        }
    }

    @Override
    public int removePdsSupplyByID (Long id)
    {
        s_logger.info ("调用 removePdsSupplyByID (Long id = 【{}】", new Object[]
        { id });
        try
        {
            return m_pdsSupplyManager.removePdsSupplyByID (id);
        }
        catch (Exception e)
        {
            s_logger.error (e.getLocalizedMessage ());
            throw new BusinessException ("删除供药信息主表出错");
        }
    }

    @Override
    public int removePdsSupplyByMap (Map <String, Object> params)
    {
        s_logger.info ("调用 removePdsSupplyByMap (Map <String, Object> params = 【{}】", new Object[]
        { params });
        try
        {
            return m_pdsSupplyManager.removePdsSupplyByMap (params);
        }
        catch (Exception e)
        {
            s_logger.error (e.getLocalizedMessage ());
            throw new BusinessException ("删除供药信息主表出错");
        }
    }

    @Override
    public void insertBatchPdsSupply (List <PdsSupply> list)
    {
        s_logger.info ("调用 insertBatchPdsSupply (List <PdsSupply> list = 【{}】", new Object[]
        { list });
        try
        {
            m_pdsSupplyManager.insertBatchPdsSupply (list);
        }
        catch (Exception e)
        {
            s_logger.error (e.getLocalizedMessage ());
            throw new BusinessException ("插入供药信息主表出错");
        }
    }

    @Override
    public List <PdsSupplyVO> findPdsSupplyListWithTotalPrice (Map <String, Object> params)
    {
        s_logger.info ("调用 findPdsSupplyListWithTotalPrice (Map <String, Object> params = 【{}】", new Object[]
        { params });
        try
        {
            List <PdsSupplyVO> list = m_pdsSupplyManager.findPdsSupplyListWithTotalPrice (params);
            if (CollectionUtils.isEmpty (list))
            {
                return new ArrayList <PdsSupplyVO> ();
            }
            String ip = (String) params.get ("ip");
            list = checkWindow (ip, list);
            return m_translatorManager.translate (list);
        }
        catch (Exception e)
        {
            s_logger.error (e.getLocalizedMessage ());
            throw new BusinessException ("查询供药信息主表出错");
        }
    }

    private List <PdsSupplyVO> checkWindow (String ip, List <PdsSupplyVO> pdsSupplyVOs)
    {
        if (null == ip)
        {
            return pdsSupplyVOs;
        }
        List <PdsSupplyVO> list = new ArrayList <PdsSupplyVO> ();
        // 根据Ip得到当前窗口号 ，本窗口的进行红色标记
        Long windowNo = null;
        PhamWindowConfig phamWindowConfig = new PhamWindowConfig ();
        phamWindowConfig.setWindowIp (ip);
        List <PhamWindowConfig> config = null;
        try
        {
            config = m_phamWindowConfigManager.findPhamWindowConfigListByExample (phamWindowConfig);
        }
        catch (Exception e)
        {
            s_logger.error (e.getMessage ());
            throw new BusinessException ("查询窗口出错,", e);
        }
        if (config.isEmpty ())
        {
            throw new BusinessException ("请给本电脑配置相应窗口！");
        }
        windowNo = config.get (0).getPhamWindowConfigId ();
        for (PdsSupplyVO pdsSupplyVO : pdsSupplyVOs)
        {
            // 如果处方对应的是本窗口给颜色标记
            if (windowNo.equals (pdsSupplyVO.getWindowNo ()))
            {
                pdsSupplyVO.setWindowFlag (AvailableEnum.YES.getEnumCode ());
            }

            list.add (pdsSupplyVO);
        }

        return list;
    }
}
